package cn.itsource.pethome.service.impl;

import cn.itsource.pethome.domain.Product;
import cn.itsource.pethome.domain.ProductDetail;
import cn.itsource.pethome.mapper.ProductMapper;
import cn.itsource.pethome.query.ProductQuery;
import cn.itsource.pethome.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ProductServiceImpl extends BaseServiceImpl<Product,ProductQuery> implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    /**
     * 重写save方法
     * @param product
     */
    @Override
    @Transactional
    public void save(Product product) {
        if(product.getId() == null){
            //先保存商品信息
            productMapper.save(product);
            //再保存商品详情信息
            ProductDetail productDetail = product.getProductDetail();
            productDetail.setProduct_id(product.getId());
            productMapper.saveProductDetail(productDetail);
        }else{
            //先修改商品信息
            productMapper.update(product);
            //再修改商品详情信息
            productMapper.updateProductDetail(product.getProductDetail());
        }
    }
}
